var common = require("./libs/common")
var web = require("./libs/web")
var db = require("./libs/db");  
var vsport = require("./libs/vsport");  
var hhuvKms = require("./libs/hhuvKmsV10");  
var appConfig = require("./config/config")

//console.log('load module web server');
var websvr = new web.TWebSvr();

/*
    db api
*/
websvr.db={
    msdb: new db.TDBMs(),
    mysqldb : new db.TDBMySql()
};
websvr.registRoute( "common" , "msdb" , "query" , function( result , request , response ){
    try{
        var appId = result.ps.appId;
        var sqlFn = result.ps.key;
        var psJson = result.ps.psJson;
        var ps = JSON.parse(psJson);
        websvr.db.msdb.query(appId , sqlFn , ps , function(res){
            result.data = res;
            response.send(JSON.stringify(result));
        })
    }
    catch(Exception){
        console.log(Exception);
        response.send(JSON.stringify(result));
    }
} );
websvr.registRoute( "common" , "mysqldb" , "query" , function( result , request , response ){
    try{
        var appId = result.ps.appId;
        var sqlFn = result.ps.key;
        var psJson = result.ps.psJson;
        var ps = JSON.parse(psJson);
        websvr.db.mysqldb.queryByFn(appId , sqlFn , ps , function(res){
            result.data = res;
            response.send(JSON.stringify(result));
        })
    }
    catch(Exception){
        console.log(Exception);
        response.send(JSON.stringify(result));
    }
} ); 


/**
 * set config info
*/
websvr.config = appConfig.config.web;
websvr.statics = appConfig.config.webStatics; 
websvr.db.msdb.apps = appConfig.config.db.msApps;
websvr.db.msdb.regist2WebSvr(websvr);
websvr.db.mysqldb.apps = appConfig.config.db.mysqlApps; 
websvr.db.mysqldb.regist2WebSvr(websvr);

/*
    hhucKmsV10
*/

websvr.hhuncKmsV10 = new hhuvKms.TKmsManager();
websvr.hhuncKmsV10.regist2WebSvr(websvr);

/*
    vsport
*/
websvr.vSport = new vsport.TVSport();
websvr.vSport.regist2WebSvr(websvr);
websvr.db.mysqldb.setConfig( websvr.vSport.config.db.dbKey , websvr.vSport.config.db.dbInfo);

websvr.start(function(){
    console.log( websvr.objs);
})